package com.zy.gmall0401.passport.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zy.gmall0401.bean.UserInfo;
import com.zy.gmall0401.service.UserService;
import com.zy.gmall0401.util.JwtUtil;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class PassportController {
    @Reference(check = false)
    UserService userService;


    //自己定义一个key
    String jwtKey = "atguigu";

    //登陆页面（关于详情页需要登录认证用到的数据originUrl）
    @GetMapping("/index")
    public String index(@RequestParam("originUrl") String originUrl, Model model){
        model.addAttribute("originUrl",originUrl);
        return "/index";
    }
//    @GetMapping("/index")
//    public String index(){
//        return "/index";
//    }

    //登陆需要传入的参数
    @PostMapping("login")
    @ResponseBody
    public String login(UserInfo userInfo, HttpServletRequest request) {

//验证用户信息
        UserInfo userInfoExist = userService.login(userInfo);
      //1
        if (userInfoExist != null) {
            //制作token
            Map<String, Object> map = new HashMap<>();
            map.put("userId", userInfoExist.getId());
            map.put("nickName", userInfoExist.getNickName());
           // System.out.println(request.getRemoteAddr());
           // 如果有 反向代理的话  ，要在反向代理中进行配置  把用户真实ip传递过来
            //获取本地电脑的IP地址
            String ipAddr = request.getHeader("X-forwarded-for");
            System.out.println("ipAddr===="+ipAddr);
            String token = JwtUtil.encode(jwtKey, map, ipAddr);

            return token;
        }


        return "fail";

    }


    //验证token里的数据 以及缓存
    //web 调用web （认证中心的模块）可以使用http的请求去调用 在拦截器中调用
    @GetMapping("verify")
    @ResponseBody
    public String verify(@RequestParam("token") String token, @RequestParam("currentIP") String currentIP,HttpServletRequest request) {
        //解码token
        String ipAddr = request.getHeader("X-forwarded-for");
        if (ipAddr.equals(currentIP)){
            Map<String, Object> userMap = JwtUtil.decode(token, jwtKey, currentIP);
            //验证缓存数据
            if(userMap!=null){
                String userId = (String) userMap.get("userId");
                Boolean boole=userService.verify(userId);
                if (boole){
                    return "success";
                }

            }
        }


       return "faile";

    }

    //测试生成token的方法  jwt生成的token可以使用base64解码
    @Test
    public void testJwt() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", "123");
        map.put("nickName", "zhang3");
        String token = JwtUtil.encode("atguigu", map, "192.168.11.120");
        System.out.println(token);
        Map<String, Object> map1 = JwtUtil.decode(token, "atguigu", "192.168.11.1260");

        System.out.println("map1===="+map1);
    }


}

